package com.project.controller.gradeController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.project.bean.LogBean;
import com.project.bean.ShopCarBean;
import com.project.bean.UserBean;
import com.project.service.LogService.ILogService;
import com.project.service.gradeService.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ILogService logService;

    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest request) throws Exception {
        UserBean user=userService.login(userName, pwd);
        if (user==null){
            return "no";
        }
        String time = LocalTime.now() + "";
        time = time.substring(0,5);
        logService.add(new LogBean("用户"+user.getCode()+"在"+ time+"登录系统"));
        request.getSession().setAttribute("loginUser", user);
        Map<Integer, ShopCarBean> purchaseMap = new HashMap<>();
        Map<Integer, ShopCarBean> saleMap = new HashMap<>();
        request.getSession().setAttribute("purchaseShopMap",purchaseMap);
        request.getSession().setAttribute("saleShopMap",saleMap);
        String userStr=new ObjectMapper().writeValueAsString(user);
        return userStr;
    }

    @RequestMapping("getUser")
    public String login( HttpServletRequest request) throws Exception {
        UserBean user=(UserBean)request.getSession().getAttribute("loginUser");
        String userStr=new ObjectMapper().writeValueAsString(user);
        return userStr;
    };

    @RequestMapping("updatePwd")
    public String updatePwd(int id,String newPwd){
        userService.updatePwd(id, newPwd);
        return "ok";
    }

    @RequestMapping("findByName")
    public PageInfo<UserBean> findByName(int pageNO, String userName){

        return userService.findByName(pageNO, userName);
    }

    @RequestMapping("add")
    public String add(@Valid UserBean userBean, BindingResult result, int[] roleId) throws Exception {
        if(result.hasErrors()){
            return new ObjectMapper().writeValueAsString(result.getFieldErrors());
        }
        userService.add(userBean, roleId);
        return "ok";
    }

    @RequestMapping("del")
    public String del(int id){
        userService.del(id);
        return "ok";
    }

    @RequestMapping("findById")
    public UserBean findById(int id){
        return userService.findById(id);
    }

    @RequestMapping("updateRole")
    public void updateRole(int id,int[] roleId){
        userService.updateRole(id, roleId);
    }
}
